#include "stdafx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////
void calc_brar(tagUMyCalcFormula*pFormula,long numCalc,phistory_data paData,double*dataCalc[10],long decNum,long sec_type,_procReflection procReflection)
{
	double seedPrice(__price_seed__[decNum]);
	long i=1,nowValue=pFormula->infoParam[0].nowValue;
//BR
	for(;i<numCalc;++i)
	{
		dataCalc[4][i] = (paData[i].highPx-paData[i-1].ClosePx)/seedPrice;
		dataCalc[5][i] = (paData[i-1].ClosePx-paData[i].lowPx)/seedPrice;
		if(dataCalc[4][i]<0) dataCalc[4][i] = 0;
		if(dataCalc[5][i]<0) dataCalc[5][i] = 0;
	}
	UMyCalc_SUM(dataCalc[2],dataCalc[4],numCalc,nowValue);
	UMyCalc_SUM(dataCalc[3],dataCalc[5],numCalc,nowValue);
	UMyCalc_DIV(dataCalc[0],dataCalc[2],dataCalc[3],numCalc);
	UMyCalc_MUL2(dataCalc[0],100,numCalc);
//AR
	for(i=0;i<numCalc;++i)
	{
		dataCalc[4][i] = (paData[i].highPx-paData[i].openPx)/seedPrice;
		dataCalc[5][i] = (paData[i].openPx-paData[i].lowPx)/seedPrice;
	}
	UMyCalc_SUM(dataCalc[2],dataCalc[4],numCalc,nowValue);
	UMyCalc_SUM(dataCalc[3],dataCalc[5],numCalc,nowValue);
	UMyCalc_DIV(dataCalc[1],dataCalc[2],dataCalc[3],numCalc);
	UMyCalc_MUL2(dataCalc[1],100,numCalc);
}

void calc_cr(tagUMyCalcFormula*pFormula,long numCalc,phistory_data paData,double*dataCalc[10],long decNum,long sec_type,_procReflection procReflection)
{
	long i=1;
	double seedPrice(__price_seed__[decNum]);
	double*CR=dataCalc[0],midData=0;
	for(;i<numCalc;++i)
	{
		midData = (paData[i-1].highPx-paData[i-1].lowPx)/seedPrice/2;
		dataCalc[4][i] = paData[i].highPx - midData;
		dataCalc[5][i] = midData - paData[i].lowPx;
		if(dataCalc[4][i]<0) dataCalc[4][i] = 0;
		if(dataCalc[5][i]<0) dataCalc[5][i] = 0;
	}
	UMyCalc_SUM(dataCalc[2],dataCalc[4],numCalc,pFormula->infoParam[0].nowValue);
	UMyCalc_SUM(dataCalc[3],dataCalc[5],numCalc,pFormula->infoParam[0].nowValue);
	UMyCalc_DIV(CR,dataCalc[2],dataCalc[3],numCalc);
	UMyCalc_MUL2(CR,100,numCalc);
	
	for(i=1;i<5;++i)
	{
		memcpy(dataCalc[5],CR,numCalc*sizeof(double));
		UMyCalc_MA(dataCalc[5],numCalc,pFormula->infoParam[i].nowValue);
		UMyCalc_REF(dataCalc[i],dataCalc[5],numCalc,1+pFormula->infoParam[0].nowValue*10/4);
	}
}
/*
MID:=REF(HIGH+LOW,1)/2;
CR:SUM(MAX(0,HIGH-MID),N)/SUM(MAX(0,MID-LOW),N)*100;
MA1:REF(MA(CR,M1),1+M1/2.5);
MA2:REF(MA(CR,M2),1+M2/2.5);
MA3:REF(MA(CR,M3),1+M3/2.5);
MA4:REF(MA(CR,M4),1+M4/2.5);
*/

void calc_vr(tagUMyCalcFormula*pFormula,long numCalc,phistory_data paData,double*dataCalc[10],long decNum,long sec_type,_procReflection procReflection)
{
	long i=1;
	double divData=0;
	double *VR=dataCalc[0];
	double *TH=dataCalc[3];
	double *TL=dataCalc[4];
	double *TQ=dataCalc[5];
	for(;i<numCalc;++i)
	{
		dataCalc[0][i] = dataCalc[1][i] = dataCalc[2][i] = 0;
		if(paData[i].ClosePx==paData[i-1].ClosePx)
			dataCalc[2][i] = (double)paData[i].totalVolumeTrade;
		else if(paData[i].ClosePx>paData[i-1].ClosePx)
			dataCalc[0][i] = (double)paData[i].totalVolumeTrade;
		else
			dataCalc[1][i] = (double)paData[i].totalVolumeTrade;
	}
	UMyCalc_SUM(TH,dataCalc[0],numCalc,pFormula->infoParam[0].nowValue);
	UMyCalc_SUM(TL,dataCalc[1],numCalc,pFormula->infoParam[0].nowValue);
	UMyCalc_SUM(TQ,dataCalc[2],numCalc,pFormula->infoParam[0].nowValue);
	UMyCalc_setinvalid(VR,numCalc);

	for(i=1;i<numCalc;++i)
	{
		if(!UMyCalc_invalid(TH[i])&&
			!UMyCalc_invalid(TL[i])&&
				!UMyCalc_invalid(TQ[i])) break;
	}
	for(;i<numCalc;++i)
	{
		divData = TL[i]*2+TQ[i];
		if(divData<0.1) VR[i] = VR[i-1];
		else VR[i] = 100*(TH[i]*2+TQ[i])/divData;
	}
	memcpy(dataCalc[1],VR,numCalc*sizeof(double));
	UMyCalc_MA(dataCalc[1],numCalc,pFormula->infoParam[1].nowValue);
}
/*
TH:=SUM(IF(CLOSE>REF(CLOSE,1),VOL,0),N);
TL:=SUM(IF(CLOSE<REF(CLOSE,1),VOL,0),N);
TQ:=SUM(IF(CLOSE=REF(CLOSE,1),VOL,0),N);
VR:100*(TH*2+TQ)/(TL*2+TQ);
MAVR:MA(VR,M);
*/

void calc_wr(tagUMyCalcFormula*pFormula,long numCalc,phistory_data paData,double*dataCalc[10],long decNum,long sec_type,_procReflection procReflection)
{
	long i=0,j=0,t=0;
	double divData=0,seedPrice(__price_seed__[decNum]);
	double*pcdHigh=dataCalc[6];
	double*pcdLow=dataCalc[7];
	double*pHHV=dataCalc[5];
	double*pLLV=dataCalc[4];
	for(i=0;i<numCalc;++i)
	{
		pcdHigh[i] = paData[i].highPx/seedPrice;
		pcdLow[i] = paData[i].lowPx/seedPrice;
	}
	for(i=0;i<2;++i)
	{
		UMyCalc_HHV(pHHV,pcdHigh,numCalc,pFormula->infoParam[i].nowValue);
		UMyCalc_LLV(pLLV,pcdLow,numCalc,pFormula->infoParam[i].nowValue);
		for(j=0;j<numCalc;++j)
		{
			if(!UMyCalc_invalid(pHHV[j])&&
				!UMyCalc_invalid(pLLV[j])) break;
		}
		for(t=0;j<numCalc;++j,++t)
		{
			divData = pHHV[j] - pLLV[j];
			if(fabs(divData)<0.0001)
			{
				if(t) dataCalc[i][j] = dataCalc[i][j-1];
			}
			else
			{
				dataCalc[i][j] = 100*(pHHV[j]-paData[j].ClosePx/seedPrice)/divData;
			}
		}
	}
}
/*
WR1:100*(HHV(HIGH,N)-CLOSE)/(HHV(HIGH,N)-LLV(LOW,N));
WR2:100*(HHV(HIGH,N1)-CLOSE)/(HHV(HIGH,N1)-LLV(LOW,N1));
*/
